<template>
  <a-modal
    v-model:visible="visible"
    title-align="start"
    width="800px"
    :title="formModel.formId? '详细': '新增'"
    :mask-closable=false
    :ok-loading=true
    :draggable=true
  >
    <a-scrollbar style="max-height:600px; overflow: auto;">
      <a-form
        id="addForm"
        ref="formModelRef"
        label-align="left"
        layout="vertical"
        :label-col-props=labelColProps
        :wrapper-col-props=wrapperColProps
        :model="formModel"
        :rules="rules"
      >
        <a-row justify="space-between">
          <a-col :span="11">
            <a-form-item field="formName" label="表单名称">
              <a-input
                v-model="formModel.formName"
                placeholder="请输入表单名称"
                allow-clear
                show-word-limit
                :max-length="15"
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item field="visible" label="表单状态">
              <a-switch
                v-model="formModel.status"
                type="round"
                :checked-value="'1'"
                :unchecked-value="'0'"
              >
                <template #checked>启用</template>
                <template #unchecked>禁用</template>
              </a-switch>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item field="remark" label="备注">
              <a-textarea
                v-model="formModel.remark"
                placeholder="请输入备注"
                auto-size
                show-word-limit
                :max-length="50"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-scrollbar>
    <template #footer>
      <a-row justify="end">
        <a-button-group>
          <a-button :disabled="loading" @click="cancel">
            取消
          </a-button>
          <a-button
            type="primary"
            :loading="loading"
            :disabled="hasUpdatePermission"
            @click="submit"
          >
            确定
          </a-button>
        </a-button-group>
      </a-row>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
import { ref, defineEmits, defineExpose, computed } from 'vue';
import { addForm, updateForm, detailForm, SysFrom } from '@/api/system/form';
import { permission } from '@/views/form/design/config';
import { labelColProps, wrapperColProps } from '@/components/dialog-add/config';
import { Message, ValidatedError } from '@arco-design/web-vue';
import { hasPermission } from '@/utils/permission';
import useLoading from '@/hooks/loading';

const emits = defineEmits(['submit']);

const { loading, setLoading } = useLoading(false);

const rules = {
  formName: [
    { required: true, message: '请输入表单名称' }
  ],
  status: [
    { required: true, message: '请选择表单状态' }
  ]
};

const formModelRef = ref();

const visible = ref<boolean>(false);

const formDisable = ref<boolean>(false);

const formModel = ref<SysFrom>({});

const hasUpdatePermission = computed(() => {
  return formModel.value.formId && !hasPermission([permission.PERMISSION_UPDATE]);
});

const submit = async () => {
  formModelRef.value.validate((errors: undefined | Record<string, ValidatedError>) => {
    if (errors) {
      return;
    }
    setLoading(true);
    if (formModel.value.formId) {
      updateForm(formModel.value).then(() => {
        submitSuccess();
      }).finally(() => {
        setLoading(false);
      });
      return;
    }
    addForm(formModel.value).then(() => {
      submitSuccess();
    }).finally(() => {
      setLoading(false);
    });
  });
};

const submitSuccess = () => {
  cancel();
  emits('submit');
  Message.success('操作成功');
};

const open = async (record: any, disable: boolean) => {
  visible.value = true;
  if (record && record.formId) {
    const { data } = await detailForm(record.formId);
    formModel.value = data;
  } else {
    formModel.value = { status: '0' };
  }
  formDisable.value = disable;
  formModelRef.value.clearValidate();
  setLoading(false);
};

const cancel = () => {
  visible.value = false;
};

defineExpose({
  open
});
</script>
<script lang="ts">
export default {
  name: 'DesignDialogAdd'
};
</script>

<style scoped>

</style>